package cn.yuanqiao.archive.supervision.service.impl;

import cn.yuanqiao.archive.supervision.domain.HdDa;
import cn.yuanqiao.archive.supervision.domain.SupervisionDa;
import cn.yuanqiao.archive.supervision.mapper.SupervisionDaMapper;
import cn.yuanqiao.archive.supervision.service.ISupervisionDaService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 督察指导档案列表Service业务层处理
 * 
 * @author xy
 * @date 2023-08-01
 */
@Service
public class SupervisionDaServiceImpl implements ISupervisionDaService 
{
    @Autowired
    private SupervisionDaMapper supervisionDaMapper;

    /**
     * 查询督察指导档案列表
     * 
     * @param ID 督察指导档案列表主键
     * @return 督察指导档案列表
     */
    @Override
    public SupervisionDa selectSupervisionDaByID(Long ID)
    {
        return supervisionDaMapper.selectSupervisionDaByID(ID);
    }

    /**
     * 查询督察指导档案列表列表
     * 
     * @param supervisionDa 督察指导档案列表
     * @return 督察指导档案列表
     */
    @Override
    public List<SupervisionDa> selectSupervisionDaList(SupervisionDa supervisionDa)
    {
        return supervisionDaMapper.selectSupervisionDaList(supervisionDa);
    }

    /**
     * 新增督察指导档案列表
     * 
     * @param supervisionDaList 督察指导档案列表
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertSupervisionDa(List<SupervisionDa> supervisionDaList)
    {
        //此处需要验证库里是否有
        for(SupervisionDa supervisionDa:supervisionDaList){
           int count=supervisionDaMapper.getCountByDaId(supervisionDa.getDaId(),supervisionDa.getTableName(),supervisionDa.getSubTableName());
           if(count>0){
               throw new ServiceException("您所选择的档案已存在!请检查!");
           }

        }


        int i=0;
        for(SupervisionDa supervisionDa:supervisionDaList){
            supervisionDa.setCreateTime(DateUtils.getNowDate());
            supervisionDa.setCreateId(SecurityUtils.getUserId());
            i+=supervisionDaMapper.insertSupervisionDa(supervisionDa);

        }

        return i;
    }

    /**
     * 修改督察指导档案列表
     * 
     * @param supervisionDa 督察指导档案列表
     * @return 结果
     */
    @Override
    public int updateSupervisionDa(SupervisionDa supervisionDa)
    {
        return supervisionDaMapper.updateSupervisionDa(supervisionDa);
    }

    /**
     * 批量删除督察指导档案列表
     * 
     * @param IDs 需要删除的督察指导档案列表主键
     * @return 结果
     */
    @Override
    public int deleteSupervisionDaByIDs(Long[] IDs)
    {
        return supervisionDaMapper.deleteSupervisionDaByIDs(IDs,new SupervisionDa().getTableName());
    }

    /**
     * 删除督察指导档案列表信息
     * 
     * @param ID 督察指导档案列表主键
     * @return 结果
     */
    @Override
    public int deleteSupervisionDaByID(Long ID)
    {
        return supervisionDaMapper.deleteSupervisionDaByID(ID);
    }

    @Override
    public List<HdDa> selectHddaList(SupervisionDa supervisionDa) {
        supervisionDa.setCreateId(SecurityUtils.getUserId());
        return supervisionDaMapper.selectHddaList(supervisionDa,SecurityUtils.getTenantCode().toUpperCase());
    }

    @Override
    public List<HdDa> selectdaList(Long daId) {
        return supervisionDaMapper.selectdaList(daId,SecurityUtils.getTenantCode().toUpperCase());
    }

    @Override
    public List<HdDa> selectHdDaList(SupervisionDa supervisionDa) {
        return supervisionDaMapper.selectHddaList(supervisionDa,SecurityUtils.getTenantCode().toUpperCase());
    }


}
